package com.tang.plugin.utils;

import com.intellij.openapi.ui.JBMenuItem;
import com.intellij.openapi.ui.JBPopupMenu;

import java.awt.*;
import java.awt.event.MouseEvent;
import java.util.Map;

public class PopMenuUtils {

    public static JBPopupMenu create(Map<String, MenuCallback> menu) {
        JBPopupMenu popupMenu = new JBPopupMenu();
        menu.forEach((k, v) -> {
            if (k.isBlank()) {
                popupMenu.addSeparator();
                return;
            }
            JBMenuItem item = new JBMenuItem(k);
            item.addActionListener((e) -> v.invoke());
            popupMenu.add(item);
        });
        return popupMenu;
    }

    public static void show(MouseEvent event, Map<String, MenuCallback> menu) {
        create(menu).show((Component) event.getSource(), event.getX(), event.getY());
    }

    public interface MenuCallback {
        void invoke();
    }

}
